package thread.demo.threadPoolExecutor;

import cn.hutool.core.lang.UUID;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorTest001 {
  public static void main(String[] args) throws InterruptedException {
    ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 5, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10), new ThreadFactory() {
      @Override
      public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName("register-user-" + UUID.fastUUID());
        System.out.println(thread.isAlive());
        return thread;
      }
    });
    
    for (int i = 0; i < 5; i++) {
      pool.execute(() -> {
        try {
          TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName());
      });
      
    }
    
    TimeUnit.SECONDS.sleep(6);
    System.out.println("-------------------------------------------");
    
    for (int j = 0; j < 5; j++) {
      pool.execute(() -> {
        try {
          TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName());
      });
    }
    
  }
}